import 'package:flutter/services.dart';
import 'package:notification_listen_service_plugin/notification_listen_body.dart';

import 'notification_listen_service_plugin_platform_interface.dart';

class MethodChannelNotificationListenServicePlugin extends NotificationListenServicePluginPlatform {
  final MethodChannel methodChannel = const MethodChannel('com.xluomo/notifications_channel');
  final EventChannel _eventChannel = const EventChannel('com.xluomo/notifications_event');

  @override
  Future<bool> requestPermission() async {
    try {
      return await methodChannel.invokeMethod('requestPermission');
    } on PlatformException catch (_) {
      return Future.value(false);
    }
  }

  @override
  Future<bool> isPermissionGranted() async {
    try {
      return await methodChannel.invokeMethod('isPermissionGranted');
    } on PlatformException catch (_) {
      return false;
    }
  }

  @override
  Future<void> listenNotification(Function(NotificationListenBody) callback) async {
    _eventChannel.receiveBroadcastStream().listen((event) {
      print("listenNotification: $event");
      callback(NotificationListenBody.fromMap(event));
    });
  }

  @override
  Future<void> sendNotification(String title, String content) async {
    return await methodChannel.invokeMethod("sendNotification", {
      "title": title,
      "content": content,
    });
  }
}
